package com.gs.m_register

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.widget.addTextChangedListener
import com.gs.common.BaseActivity
import com.gs.common.constant.ActionConst
import com.gs.common.util.CheckPhoneNumUtil
import com.gs.common.util.ToastUtil
import com.gs.m_register.databinding.RegisterLayoutBinding

class RegisterActivity : BaseActivity(), RegisterPersistent.OnDoRegisterStateCallback {

    private lateinit var binding: RegisterLayoutBinding
    private val areEditTextsFilled = arrayOf(false, false, false, false, false) // 第1个标志表示后4个edt是否全部被填充
    private val registerPersistent by lazy { RegisterPersistent.getInstance(this) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = RegisterLayoutBinding.inflate(layoutInflater)
        setContentView(binding.root)

        //当所有编辑框被填充后，激活按钮
        super.activeButton(
            binding.registerBtn,
            arrayOf(binding.rgTelNumEdt, binding.rgUsernameEdt, binding.rgPasswordEdt, binding.rgRePasswordEdt),
            areEditTextsFilled,
            resources.getDrawable(R.drawable.register_btn_bg),
            R.color.register_btn_color,
            resources.getDrawable(R.drawable.register_btn_active_bg),
            R.color.register_btn_active_color
        )

        binding.rgTelNumEdt.addTextChangedListener {
            if (it != null){
                if (!CheckPhoneNumUtil.isValidTelNumber(it.toString())) {
                    binding.rgTelNumEdt.setTextColor(resources.getColor(com.gs.common.R.color.red))
                } else {
                    binding.rgTelNumEdt.setTextColor(resources.getColor(com.gs.common.R.color.black))
                }
            }
        }

        binding.rgUsernameEdt.addTextChangedListener {
            if (it != null) {
                if ((it.isNotEmpty() && !it[0].isLetter()) || it.length < 3) {
                    binding.rgUsernameEdt.setTextColor(resources.getColor(com.gs.common.R.color.red))
                } else {
                    binding.rgUsernameEdt.setTextColor(resources.getColor(com.gs.common.R.color.black))
                }
            }
        }

        binding.rgPasswordEdt.addTextChangedListener {
            if (it != null) {
                if (it.length < 8) {
                    binding.rgPasswordEdt.setTextColor(resources.getColor(com.gs.common.R.color.red))
                } else {
                    binding.rgPasswordEdt.setTextColor(resources.getColor(com.gs.common.R.color.black))
                }
            }
        }

        binding.rgRePasswordEdt.addTextChangedListener {
            if (it != null) {
                if (it.toString() != binding.rgPasswordEdt.text.toString()) {
                    binding.rgRePasswordEdt.setTextColor(resources.getColor(com.gs.common.R.color.red))
                } else {
                    binding.rgRePasswordEdt.setTextColor(resources.getColor(com.gs.common.R.color.black))
                }
            }
        }
    }

    fun close(view: View) {
        this.finish()
    }

    fun register(view: View) {
        if (areEditTextsFilled[0]) {
            val telNum = binding.rgTelNumEdt.text.toString().trim()
            val username = binding.rgUsernameEdt.text.toString().trim()
            val password = binding.rgPasswordEdt.text.toString().trim()
            val rePassword = binding.rgRePasswordEdt.text.toString().trim()
            if (!CheckPhoneNumUtil.isValidTelNumber(telNum)) {
                ToastUtil.showShortToast(this, "无效手机号码！")
                return
            }
            if (username.length < 3) {
                ToastUtil.showShortToast(this, "用户名的长度不能小于3！")
                return
            }
            if (!username[0].isLetter()) {
                ToastUtil.showShortToast(this, "用户名请以英文字母开头！")
                return
            }
            if (password.length < 8) {
                ToastUtil.showShortToast(this, "密码的长度不能小于8！")
                return
            }
            if (password != rePassword) {
                ToastUtil.showShortToast(this, "两次输入的密码不一致，请重新输入！")
                return
            }
            registerPersistent.register(telNum, username, password, this)
        } else {
            ToastUtil.showShortToast(this, "请输入完整信息")
        }
    }

    fun sendVerificationCode(view: View) {
        ToastUtil.showShortToast(this, "发送验证码")
    }

    override fun onRegisterSuccess() {
        ToastUtil.showShortToast(this, "注册成功！")
        val intent = Intent().apply {
            action = ActionConst.TO_HOME_ACTIVITY_ACTION
        }
        startActivity(intent)
        this.finish()
    }

    override fun onRegisterFailed() {
        ToastUtil.showShortToast(this, "注册失败，手机号或用户名已被使用！")
    }

    override fun onNetworkError() {
        ToastUtil.showShortToast(this, "无法连接网络！")
    }

    override fun onServerError() {
        ToastUtil.showShortToast(this, "无法连接服务器！")
    }

    override fun onUnknownError() {
        ToastUtil.showShortToast(this, "未知错误！")
    }
}